﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using DevExpress.XtraReports.UI;
using DevExpress.XtraReports.UserDesigner;

namespace BHair.Report
{
    public partial class frmjhmx : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        public frmjhmx()
        {
            InitializeComponent();
        }
        string constr = ConfigurationManager.ConnectionStrings["BHair.Properties.Settings.BHairDBConnectionString"].ConnectionString;
        private void button1_Click(object sender, EventArgs e)
        {
            if (txtspname.Text == string.Empty) //如果商品名称为空
            {
                timejinhuo();
            }
            else if (txtspname.Text != string.Empty)  //当商品名称不为空
            {
                spnameselect();
            }
        }
        #region 按时间查询
        public DataSet timejinhuo() 
        {
            DataSet ds = new DataSet();
            DataTable dt=new DataTable();
            SqlConnection conn = new SqlConnection(constr);
            try 
            {
                conn.Open();
                string sql = string.Format("SELECT [djid] as 单据编号,[spid] as 商品编号,[spname] as 商品名称,[spjhj] as 进货单价,[spjhs] as 进货数量,[spjhtime] as 进货日期,[spgys] as 供应商,[spjsr] as 经手人,[spjhje] as 进货金额,[spjhbz] as 单据备注 FROM [BHairDB].[dbo].[jh] where '{0}'<=jh.spjhtime and jh.spjhtime<='{1}'",this.dtpbegin.Text,this.dtpend.Text);
                string sql1 = string.Format("SELECT [djid],[spid] ,[spname] ,[spjhj],[spjhs],[spjhtime] ,[spgys],[spjsr] ,[spjhje] ,[spjhbz]  FROM [BHairDB].[dbo].[jh] where '{0}'<=jh.spjhtime and jh.spjhtime<='{1}'", this.dtpbegin.Text, this.dtpend.Text);
                SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
                SqlDataAdapter sda1 = new SqlDataAdapter(sql1,conn);

                sda.Fill(dt);
                sda1.Fill(ds,"jh");
                this.dataGridView1.DataSource = dt;
                conn.Close();
            }
            catch(Exception ex)
            {
                conn.Close();
                MessageBox.Show(ex.Message);
            }

            return ds;
        }
        #endregion
        #region 按商品名称和时间查询
        public DataSet spnameselect() 
        {
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            SqlConnection conn = new SqlConnection(constr);
            try
            {
                conn.Open();
                string sql = string.Format("SELECT [djid] as 单据编号,[spid] as 商品编号,[spname] as 商品名称,[spjhj] as 进货单价,[spjhs] as 进货数量,[spjhtime] as 进货日期,[spgys] as 供应商,[spjsr] as 经手人,[spjhje] as 进货金额,[spjhbz] as 单据备注 FROM [BHairDB].[dbo].[jh] where '{0}'<=jh.spjhtime and jh.spjhtime<='{1}' and jh.spname like '%{2}%'", dtpbegin.Text, dtpend.Text, txtspname.Text);
                SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
                sda.Fill(dt);
                this.dataGridView1.DataSource = dt;
                conn.Close();
            }
            catch (Exception ex) 
            {
                conn.Close();
                MessageBox.Show(ex.Message);
            }
            return ds;
        }
        #endregion

        private void button2_Click(object sender, EventArgs e)
        {
            DevExpress.XtraReports.UI.XtraReport xr = new DevExpress.XtraReports.UI.XtraReport();
            xr.LoadLayout(AppDomain.CurrentDomain.BaseDirectory+ @"bb\Reportjhmx.repx");
            xr.DataSource = timejinhuo();
            xr.ShowPreview();

        }

        private void button3_Click(object sender, EventArgs e)
        {
            DevExpress.XtraReports.UI.XtraReport xr = new DevExpress.XtraReports.UI.XtraReport();
            xr.LoadLayout(AppDomain.CurrentDomain.BaseDirectory + @"bb\Reportjhmx.repx");
            xr.Report.Name = "Reportjhmx";
            //xr.DataSource = timejinhuo();
            xr.ShowDesignerDialog();
            xr.SaveLayout(AppDomain.CurrentDomain.BaseDirectory + @"bb\Reportjhmx.repx");
            xr.Dispose();
        }

        private void frmjhmx_Load(object sender, EventArgs e)
        {

        }
    }
}
